package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjVideo;
import com.zb.entity.ZjVideoContent;
import com.zb.mapper.ZjVideoContentMapper;
import com.zb.service.ZjVideoContentService;
import com.zb.vo.VideoPathVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品视频内容表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-31
 */
@Service
public class ZjVideoContentServiceImpl extends ServiceImpl<ZjVideoContentMapper, ZjVideoContent> implements ZjVideoContentService {
    @Resource
    private ZjVideoContentMapper videoContentMapper;

    @Override
    public void save(Long custAccId, Long videoId, List<VideoPathVo> videoPaths, List<VideoPathVo> delVideoPaths) {
        if (null != delVideoPaths &&! delVideoPaths.isEmpty()) {
            this.removeByIds(delVideoPaths.stream().map(VideoPathVo::getId).toList());
        }
        if (null == videoPaths || videoPaths.isEmpty()) {
            return;
        }
        List<ZjVideoContent> contents = new ArrayList<>();
        videoPaths.forEach(t -> {
            if (null == t.getId()) {
                contents.add(new ZjVideoContent()
                        .setId(IdWorker.getId())
                        .setCustAccId(custAccId)
                        .setVideoId(videoId)
                        .setImage(t.getPath())
                );
            }
        });
        if (!contents.isEmpty()) {
            this.saveBatch(contents);
        }
    }

    // 根据视频ID获取视频信息
    @Override
    public List<ZjVideoContent> getImageByVidelId(Long videoId) {
        QueryWrapper<ZjVideoContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "image");
        wrapper.eq("video_id", videoId);
        return this.list(wrapper);
    }

    // 根据视频ID删除所有视频内容
    @Override
    public void deleteByVideoId(List<Long> videoIdList) {
        videoContentMapper.deleteByVideoIds(videoIdList);
    }
}
